package com.liulin.cdse.controller.admin;

import com.liulin.cdse.config.MinioConfig;
import com.liulin.cdse.entity.Category;
import com.liulin.cdse.entity.Goods;
import com.liulin.cdse.pojo.GoodsQO;
import com.liulin.cdse.service.CategoryService;
import com.liulin.cdse.service.GoodsService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioClient minioClient;

    @GetMapping("add")
    public String add(Model model) {
        //查询所有的类别
        List<Category> categories = categoryService.list();
        model.addAttribute("categories", categories);
        return "goods_add";
    }

    @Value("${images.uploadpath}")
    private String uploadPath;

    @PostMapping("add")
    public String add(Goods goods, MultipartFile file) {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "");
//        File dest = new File(uploadPath + File.separator + newFileName + suffix);
//        try {
//            file.transferTo(dest);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        try{
            PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(newFileName)
                    .stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
            //文件名称相同会覆盖
            minioClient.putObject(objectArgs);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        log.debug("Goods:{}", goods);
//        goods.setImage("http://localhost:8080/images/" + newFileName + suffix);
        String path = minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + newFileName;
        goods.setImage(path);
        goodsService.add(goods);
        return "redirect:/admin/goods/listbypage";
    }

    @GetMapping("listbypage")
    public String listByPage(Integer pageNo, GoodsQO goodsQO, Model model) {
        log.debug("GoodsQO:{}", goodsQO);
        int page = 1;
        if (pageNo != null) {
            page = pageNo;
        }
        model.addAttribute("pageBean", goodsService.findByPage(page, goodsQO));
        List<Category> categories = categoryService.list();
        model.addAttribute("categories", categories);

        return "goods_list";
    }

}
